package com.powernode.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.SysLog;
import com.powernode.model.Result;

import com.powernode.service.SysLogService;
import io.swagger.annotations.Api;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 系统操作日志管理控制层
 *
 * @return
 */
@Api(tags = "系统操作日志接口管理")
@RequestMapping("sys/log")
@RestController
public class SysLogController {

    @Autowired
    private SysLogService sysLogService;

    /**
     * 多条件分页查询系统操作日志
     * @param current 当前页码
     * @param size 每页显示条数
     * @param userId 用户ID，可选参数，用于条件查询
     * @param operation 操作描述，可选参数，用于条件查询
     * @return 返回分页查询结果，包含系统操作日志列表和分页信息
     */
    @ApiOperation("多条件分页查询系统操作日志")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('sys:log:page')")
    public Result<Page<SysLog>> loadSysLogPage(@RequestParam Long current,
                                               @RequestParam Long size,
                                               @RequestParam(required = false) Long userId,
                                               @RequestParam(required = false) String operation) {
        // 创建分页对象page
        Page<SysLog> page = new Page<>(current, size);
        // 多条件分页查询系统操作日志
        page = sysLogService.page(page, new LambdaQueryWrapper<SysLog>()
                .eq(ObjectUtil.isNotEmpty(userId), SysLog::getUserId, userId)
                .like(StringUtils.hasText(operation), SysLog::getOperation, operation)
                .orderByDesc(SysLog::getCreateDate));
        // 返回查询结果
        return Result.success(page);
    }

}
